#!/bin/bash
#Python命令行路径
pythoncmd="/usr/bin/python3"

#填写阿里云的 AccessKey ID及 AccessKey Secret
ALY_KEY=""
ALY_TOKEN=""

#填写腾讯云的SecretId及SecretKey
TXY_KEY=""
TXY_TOKEN=""

#填写华为云的 Access Key Id(ak) 及 Secret Access Key(sk)
HWY_KEY=""
HWY_TOKEN=""

PATH=$(
  cd $(dirname $0)
  pwd
)


# 第1个参数：使用那个 DNS 的 API
# 第2个参数：add or clean
pdns=$1    #aly, txy, hwy
paction=$2 #add or clean

#内部变量
cmd=""
key=""
token=""

if [[ "$paction" != "clean" ]]; then
  paction="add"
fi

cmd=$pythoncmd
if [[ "$pdns" == "aly" ]]; then
    dnsapi=$PATH"/python-version/dns.py aly"
    key=$ALY_KEY
    token=$ALY_TOKEN
elif [[ "$pdns" == "txy" ]]; then
    dnsapi=$PATH"/python-version/dns.py txy"
    key=$TXY_KEY
    token=$TXY_TOKEN
elif [[ "$pdns" == "hwy" ]]; then
    dnsapi="$PATH/python-version/dns.py hwy"
    key=$HWY_KEY
    token=$HWY_TOKEN
else
    echo "Not support this dns services"
    exit
fi

#STR="$cmd $dnsapi $paction $CERTBOT_DOMAIN _acme-challenge $CERTBOT_VALIDATION $key $token"
#echo $STR

$cmd $dnsapi $paction $CERTBOT_DOMAIN "_acme-challenge" $CERTBOT_VALIDATION $key $token >>"/etc/letsencrypt/au.log"

if [[ "$paction" == "add" ]]; then
  # DNS TXT 记录刷新时间
  /bin/sleep 30
fi
